<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/15 0015
 * Time: 10:12
 */
namespace Wx\Controller;
use Think\Controller;
use Wx\Model;
class AuthenticationController extends CommonController
{
    public function index(){
        $id = session('u_id');
        $area = M('area')->where('lev = 1')->select();//获取一级地址
        $member = M("member")->where("id = {$id}")->find();//获取用户信息
        // 省市区信息+小区信息
		if($member['c_id']>0){
			$c_info = M('shop_community')->where(array('id' => $member['c_id'] ))->find();
			$c_info['a_1_name'] = M('area')->where('id='.$c_info['a_id_1'])->getField('name');
			$c_info['a_2_name'] = M('area')->where('id='.$c_info['a_id_2'])->getField('name');
			$c_info['a_3_name'] = M('area')->where('id='.$c_info['a_id_3'])->getField('name');
		}
		//地址js生成
		$ali=set_area_js();
        $this->assign('ali',$ali);
		//地址js生成-end
        $this->assign($member);
        $this->assign('area',$area);
        $this->assign('c_info',$c_info);
		if($_GET['do_what']=='binding'){
			//首页只绑定小区,心愿团支付成功只绑定小区$pay，即表示从心愿团跳转过来
            $pay = intval(I('pay'));
            $this->assign('pay',$pay);
			$this->display('authentication-binding');
		}else{
			//绑定小区有自提门店的显示出来
			$shop_store=D('shop_store');
			if($member['c_id']>0){
				$s_info=$shop_store->where("c_id=".$member['c_id'])->find();
				$this->assign('s_info',$s_info);
			}
			$this->display('authentication');
		}
        
    }//end fun Index()

    public function sendsms(){
        $mobile = I('mobile');
        if ($mobile != ''){
            $res = D("Sms")->send_message("$mobile");//调用发送短信的方法
            if ($res['state'] > 0){
                $this->ajaxReturn(array('state'=>$res['state'],'value'=>$res['value']),"JSON");//短信发送错误返回
            }else{
                $this->ajaxReturn(array('state'=>$res['state'],'code'=>$res['code'],'value'=>$res['value']),"JSON");//短信发送成功返回
            }
        }else{
            $this->ajaxReturn(array('state'=>404,'value'=>'请填写手机号'),"JSON");//错误返回
        }
    }//end fun sendsms()

    public function edit(){
        //$post = I('post.');
		$post =$_POST;
		$a_id=explode('-',$post['a_id']);//dump($a_id);die; 
		$post['a_id_1']=(int)$a_id[0]; 
		$post['a_id_2']=(int)$a_id[1];
		$post['a_id_3']=(int)$a_id[2];
		//dump($post);die;
        $u_id = session("u_id");//获取登录用户的id
		
        if($_POST){
            $post['c_id'] = preg_replace('/ /', '',$post['c_id']);//过滤字段中的空格
			
            //查询用户填写的地址是否存在
			$where="a_id_1=".$post['a_id_1']." AND a_id_2=".$post['a_id_2']." AND a_id_3=".$post['a_id_3']." AND title = '".$post['c_id']."'";
            $shop_community=D("shop_community");
            $commiunty =$shop_community->where($where)->find();//dump($commiunty);die;
            //更新到member表的数据
            $savedata = array(
                'c_id' => '',//用户绑定的小区id
                'realname' => $post['realname'],//用户的真实姓名
                'mobile' => $post['mobile'],//用户的手机号
                'pay_pwd' => md5($post['pay_pwd']),//卡额支付密码
                'hometown' => $post['hometown'],//用户的家乡
                'my_addr' => $post['my_addr'],//用户的详细地址
            );
            if ($commiunty['id']>0){
                $savedata['c_id'] = $commiunty['id'];
            }else{
                //添加的小区信息
                $addc = array(
                    "a_id_1" => $post['a_id_1'],//省
                    "a_id_2" => $post['a_id_2'],//市
                    "a_id_3" => $post['a_id_3'],//区
                    "title" => $post['c_id'],//小区名
                );
                //添加对应小区
                $addcommiunty = $shop_community->add($addc);//dump($addcommiunty);die;
                $savedata['c_id'] = $addcommiunty;
            }//end if $commiunty
            //更新member表的数据
			//dump($savedata);die;
            $updatemember = M("member")->where("id = {$u_id}")->save($savedata);
			
			/*
            //插入一条默认地址
            $address_arr = array(
                'u_id' => $u_id,
                'name' => $post['realname'],
                'mobile' => $post['mobile'],
                'commiunty_id' => $post['c_id'],
                'status' => 1,
                'is_default' => 1,
                'addtime' => time()
            );
            //插入address表-默认地址
            $add_address = M("address")->add($address_arr);
			*/
            //if ($updatemember>0){
                //如果是心愿团跳转过来的，绑定成功后，跳转回去。
                if($post['is_wish_pay']==1){
                    $this->redirect("/Wx/WishGroup/index/#Number");
                }
                if(session("pro_url")!=''){
					redirect(session("pro_url"));
				}else{
					$this->redirect("/Wx/User/");
				}
            //}
        }//end if post
    }//end fun edit()
	//首页绑定小区
    public function edit_binding(){
        //$post = I('post.');
		$post =$_POST;
		$a_id=explode('-',$post['a_id']);//dump($a_id);die; 
		$post['a_id_1']=(int)$a_id[0]; 
		$post['a_id_2']=(int)$a_id[1];
		$post['a_id_3']=(int)$a_id[2];
		//dump($post);die;
        $u_id = session("u_id");//获取登录用户的id
		
        if($_POST){
            $post['c_id'] = preg_replace('/ /', '',$post['c_id']);//过滤字段中的空格
			
            //查询用户填写的地址是否存在
			$where="a_id_1=".$post['a_id_1']." AND a_id_2=".$post['a_id_2']." AND a_id_3=".$post['a_id_3']." AND title LIKE '".$post['c_id']."'";
            $shop_community=D("shop_community");
            $commiunty =$shop_community->where($where)->find();
            //更新到member表的数据
            $savedata = array(
                'c_id' => '',//用户绑定的小区id
                'my_addr' => $post['my_addr'],//用户的详细地址
            );
            if ($commiunty['id']>0){
                $savedata['c_id'] = $commiunty['id'];
            }else{
                //添加的小区信息
                $addc = array(
                    "a_id_1" => $post['a_id_1'],//省
                    "a_id_2" => $post['a_id_2'],//市
                    "a_id_3" => $post['a_id_3'],//区
                    "title" => $post['c_id'],//小区名
                );
                //添加对应小区
                $addcommiunty = $shop_community->add($addc);
                $savedata['c_id'] = $addcommiunty;
            }//end if $commiunty
            //更新member表的数据
            $updatemember = M("member")->where("id = {$u_id}")->save($savedata);
			/*
            //插入一条默认地址
            $address_arr = array(
                'u_id' => $u_id,
                'name' => $post['realname'],
                'mobile' => $post['mobile'],
                'commiunty_id' => $post['c_id'],
                'status' => 1,
                'is_default' => 1,
                'addtime' => time()
            );
            //插入address表-默认地址
            $add_address = M("address")->add($address_arr);
			*/
            if ($updatemember>0){
                //如果是心愿团跳转过来的，绑定成功后，跳转回去。
                if($post['is_wish_pay']==1){
                    $this->redirect("/Wx/WishGroup/index/#Number");
                }
                if(session("pro_url")){
					redirect(session("pro_url"));
				}else{
					$this->redirect("/Wx/User/");
				}
            }
        }//end if post
    }//end fun edit_binding()
	//首页绑定小区
	
}//end class